package com.example.onlinemusic.controller;

import com.example.onlinemusic.mapper.LoveMusicMapper;
import com.example.onlinemusic.model.Music;
import com.example.onlinemusic.model.User;
import com.example.onlinemusic.tools.Constant;
import com.example.onlinemusic.tools.ResponseBodyMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LXW
 * Date: 2022-07-31
 * Time: 13:10
 */
@RestController
@RequestMapping("/lovemusic")
public class LoveMusicController {
    @Autowired
    private LoveMusicMapper loveMusicMapper;

    @RequestMapping("/likemusic")
    public ResponseBodyMessage<Boolean> likeMusic(@RequestParam String id, HttpServletRequest request) {
        int music_id = Integer.parseInt(id);
        System.out.println("user_id: " + music_id);

        // 检查是否登录 (没有session就不创建)
        HttpSession httpSession = request.getSession(false);
        if (httpSession == null || httpSession.getAttribute(Constant.USERINFO_SESSION_KEY) == null) {
            System.out.println("没有登录！");
            return new ResponseBodyMessage<>(-1, "没有登录！", false);
        }
        User user = (User) httpSession.getAttribute(Constant.USERINFO_SESSION_KEY);
        int user_id = user.getId();
        System.out.println("user_id: " + user_id);
        Music music = loveMusicMapper.findLoveMusicByUserIdAndMusicId(user_id, music_id);
        if(music != null) {
            return new ResponseBodyMessage<>(-1, "用户已收藏过该音乐", false);
        }else {
            boolean effect = loveMusicMapper.insertLoveMusic(user_id, music_id);
            if(effect) {
                return new ResponseBodyMessage<>(200, "收藏音乐成功", true);
            }else {
                return new ResponseBodyMessage<>(-1, "收藏音乐失败", false);
            }
        }
    }

    @RequestMapping("/findlovemusic")
    public ResponseBodyMessage<List<Music>> findLoveMusic(@RequestParam(required = false) String musicName,HttpServletRequest request) {
        // 检查是否登录 (没有session就不创建)
        HttpSession httpSession = request.getSession(false);
        if (httpSession == null || httpSession.getAttribute(Constant.USERINFO_SESSION_KEY) == null) {
            System.out.println("没有登录！");
            return new ResponseBodyMessage<>(-1, "没有登录！", null);
        }
        User user = (User) httpSession.getAttribute(Constant.USERINFO_SESSION_KEY);
        int user_id = user.getId();
        System.out.println("user_id: " + user_id);
        List<Music> musics = null;
        if(musicName != null) {
            musics = loveMusicMapper.findLoveMusicByKeyAndUID(musicName, user_id);
        }else {
            musics = loveMusicMapper.findLoveMusicByUserId(user_id);
        }
        return new ResponseBodyMessage<>(200, "查询到了所有收藏的音乐", musics);
    }

    @RequestMapping("/deletelovemusic")
    public ResponseBodyMessage<Boolean> deleteLoveMusic(@RequestParam String id, HttpServletRequest request) {
        int music_id = Integer.parseInt(id);
        // 检查是否登录 (没有session就不创建)
        HttpSession httpSession = request.getSession(false);
        if (httpSession == null || httpSession.getAttribute(Constant.USERINFO_SESSION_KEY) == null) {
            System.out.println("没有登录！");
            return new ResponseBodyMessage<>(-1, "没有登录！", null);
        }
        User user = (User) httpSession.getAttribute(Constant.USERINFO_SESSION_KEY);
        int user_id = user.getId();
        System.out.println("user_id: " + user_id);
        int ret = loveMusicMapper.deleteLoveMusic(user_id, music_id);
        if(ret == 1) {
            return new ResponseBodyMessage<>(200, "取消收藏成功", true);
        }else {
            return new ResponseBodyMessage<>(-1, "取消收藏失败", false);
        }
    }
}
